//
//  Brain.swift
//  Maze24
//
//  Created by Mars on 2020/11/7.
//

import Foundation

enum Brain {
  case left(Int)
  case leftOp(Int, Op)
  case none
}

extension Brain {
  var currentValue: Int {
    switch self {
    case .left(let n):
      return n
    default:
      return -1
    }
  }
  
  func apply(item: ButtonItem) -> Brain {
    switch item.category {
    case .number(let n):
      return acceptNumber(n)
    case .op(let op):
      return acceptOp(op)
    default:
      return self
    }
  }
  
  func acceptNumber(_ n: Int) -> Brain {
    switch self {
    case .leftOp(let l, let op):
      return .left(op.calculate(l: l, r: n))
    default:
      return .left(n)
    }
  }
  
  func acceptOp(_ op: Op) -> Brain {
    switch self {
    case .left(let l):
      return .leftOp(l, op)
    case .leftOp(let l, _):
      return .leftOp(l, op)
    default:
      return self
    }
  }
}

extension Brain: Equatable {}
